﻿using System; using HISModels.ViewModel;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using KeanuMvc.Reference;
using HIS.Utils;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using HISModels.Entities;
using System.Configuration;
using HIS.Utils.Securities;

namespace HIS.Controllers.Purchasing {

    public class PurchaseRequisition : HIS.Controllers.AppBaseController 
    
    {

        #region Fields
        private string strAction = string.Empty;
        private string strNotification = string.Empty;
        private StringReader stringReader = null;
        #endregion

        public PurchaseRequisition() {
            Data.Add("Pagetitle", "Purchase Request");
            Data.Add("Notification", strNotification);
        }

        [Authorization(RoleAction = "purchasing-purchaserequisition")]
        public override void Index() {
            try {
                Data["Pagetitle"] += " :: Purchase Request";
                RenderFile("Purchasing\\purchaserequisition.html");
            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            }
        }

        [Authorization(RoleAction = "purchasing-purchaserequisition")]
        public void EditPurchaseRequisition() {
            try {
                Data["Pagetitle"] += " :: Purchase Request";
                RenderFile("Purchasing\\editpurchaserequisition.html");
            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            }
        }

        [Authorization(RoleAction = "purchasing-purchaserequisition")]
        public void TableListPurchaseReqMaster() {


            try {
                string strJson = string.Empty;
                string sEcho = HttpContext.Current.Request["sEcho"];
                string startdate = HttpContext.Current.Request["startdate"];
                string enddate = HttpContext.Current.Request["enddate"];
                string prnumber = HttpContext.Current.Request["prnumber"];


                string ssearch = "startdate=" + startdate + ";enddate=" + enddate + ";prnumber=" + prnumber;

                PostDataTablesNet pagingSearch = new PostDataTablesNet();
                pagingSearch.iDisplayLength = Convert.ToInt32(HttpContext.Current.Request["iDisplayLength"]);
                pagingSearch.iDisplayStart = Convert.ToInt32(HttpContext.Current.Request["iDisplayStart"]);
                pagingSearch.sEcho = Convert.ToInt32(HttpContext.Current.Request["sEcho"]);
                pagingSearch.sSearch = ssearch;

                string strUri = Global.ServiceUriDictionary["purchasepaging"] + Global.ServiceUriDictionary["requestformat"];
                stringReader = base.RestService.PostJson(strUri, JsonConvert.SerializeObject(pagingSearch));
                ///Convert to JObject
                ///  
                JObject o = JObject.Parse(stringReader.ReadToEnd());
                JArray aaData = (JArray)o["aaData"];
                ///Modify it
                JArray aaDataModified = new JArray();
                for (int i = 0; i < aaData.Count(); i++) {
                    JObject oo = JObject.Parse(aaData[i].ToString());
                    oo["purchaseid"] = KeanuMvc.Reference.CryptoString.Encrypt(oo["purchaseid"].ToString());
                    DateTime releasedate = (DateTime)oo["ReleasedDate"];
                    oo["ReleasedDate"] = releasedate.ToShortIndonesian(false);                
                    aaDataModified.Add(oo);

                }
                o["aaData"] = aaDataModified;

                ///So serialize
                strJson = JsonConvert.SerializeObject(o);

                HttpContext.Current.Response.Write(strJson);

            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            } finally {
                if (stringReader != null) stringReader.Dispose();
                base.RestService.Dispose();
            }
        }

        [Authorization(RoleAction = "purchasing-purchaserequisition")]
        public void SearchItem() {
            try {
                Data["Pagetitle"] += " :: Purchase Request";
                RenderFile("Purchasing\\searchitem.html");
            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            }
        }

         [Authorization(RoleAction = "purchasing-purchaserequisition")]
        public void TableListSearchitem() {
 
            try {
                string itemcode = HttpContext.Current.Request["itemcode"].NullSafeToString();
                string itemname = HttpContext.Current.Request["itemname"].NullSafeToString();
                string ssearch = "code=" + itemcode + ";nama=" + itemname;

                PostDataTablesNet pagingSearch = new PostDataTablesNet();
                pagingSearch.iDisplayLength = Convert.ToInt32(HttpContext.Current.Request["iDisplayLength"]);
                pagingSearch.iDisplayStart = Convert.ToInt32(HttpContext.Current.Request["iDisplayStart"]);
                pagingSearch.sEcho = Convert.ToInt32(HttpContext.Current.Request["sEcho"]);
                pagingSearch.sSearch = ssearch;

                string strUri = Global.ServiceUriDictionary["itempaging"] + Global.ServiceUriDictionary["requestformat"];
                StringReader stringReader = base.RestService.PostJson(strUri, JsonConvert.SerializeObject(pagingSearch));
                string strJson = stringReader.ReadToEnd();
               
                HttpContext.Current.Response.Write(strJson);
            } catch (Exception ex) {
                HttpContext.Current.Response.Write(ex.Message);
            } finally {
                if (stringReader != null) stringReader.Dispose();
                base.RestService.Dispose();
            }
        }

         [Authorization(RoleAction = "purchasing-purchaserequisition")]
         public void TableListPRDetail() {


             try {
                 string itemcode = HttpContext.Current.Request["itemcode"].NullSafeToString();
                 string itemname = HttpContext.Current.Request["itemname"].NullSafeToString();
                 string ssearch = "code=" + itemcode ;

                 PostDataTablesNet pagingSearch = new PostDataTablesNet();
                 pagingSearch.iDisplayLength = Convert.ToInt32(HttpContext.Current.Request["iDisplayLength"]);
                 pagingSearch.iDisplayStart = Convert.ToInt32(HttpContext.Current.Request["iDisplayStart"]);
                 pagingSearch.sEcho = Convert.ToInt32(HttpContext.Current.Request["sEcho"]);
                 pagingSearch.sSearch = ssearch;

                 string strUri = Global.ServiceUriDictionary["purchasedetailpaging"] + Global.ServiceUriDictionary["requestformat"];
                 StringReader stringReader = base.RestService.PostJson(strUri, JsonConvert.SerializeObject(pagingSearch));
                 string strJson = stringReader.ReadToEnd();
                
                 HttpContext.Current.Response.Write(strJson);
             } catch (Exception ex) {
                 HttpContext.Current.Response.Write(ex.Message);
             } finally {
                 if (stringReader != null) stringReader.Dispose();
                 base.RestService.Dispose();
             }

             
         }

         [Authorization(RoleAction = "purchasing-purchaserequisition")]
         public void CreatePurchaseRequisition()
         {
             try
             {
                 Data["Pagetitle"] += " :: Purchase Request";
                 RenderFile("purchasing\\createpurchase.html");
             }
             catch (Exception ex)
             {
                 HttpContext.Current.Response.Write(ex.Message);
             }
         }



       
    }
}